package io.renren.modules.eightapp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.eightapp.dto.EiRepairDTO;
import io.renren.modules.eightapp.dto.EiRepairInfoDTO;
import io.renren.modules.eightapp.entity.EiRepairInfoEntity;
import io.renren.modules.eightapp.excel.EiRepairInfoExcel;
import io.renren.modules.eightapp.service.EiRepairInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-27
 */
@RestController
@RequestMapping("eightapp/eirepairinfo")
@Api(tags="")
public class EiRepairInfoController {
    @Autowired
    private EiRepairInfoService eiRepairInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("eightapp:eirepairinfo:page")
    public Result<PageData<EiRepairInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<EiRepairInfoDTO> page = eiRepairInfoService.page(params);

        return new Result<PageData<EiRepairInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("eightapp:eirepairinfo:info")
    public Result<EiRepairInfoDTO> get(@PathVariable("id") Long id){
        EiRepairInfoDTO data = eiRepairInfoService.get(id);

        return new Result<EiRepairInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("eightapp:eirepairinfo:save")
    public Result save(@RequestBody EiRepairInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        Timestamp time= new Timestamp(System.currentTimeMillis());
        dto.setUpdateTime(time);
        dto.setCreateTime(time);
        dto.setCreator("admin");

        eiRepairInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("eightapp:eirepairinfo:update")
    public Result update(@RequestBody EiRepairInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        Timestamp time= new Timestamp(System.currentTimeMillis());
        dto.setUpdateTime(time);

        eiRepairInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("eightapp:eirepairinfo:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        eiRepairInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("eightapp:eirepairinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<EiRepairInfoDTO> list = eiRepairInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, EiRepairInfoExcel.class);
    }


    @PutMapping("/{id}")
    @ApiOperation("修改处理状态")
    public Result<Integer> updateStateById(@PathVariable("id") Integer id) {
        int i = eiRepairInfoService.updateStateById(id);
        return new Result<Integer>().ok(i);
    }

    @GetMapping("/getRepairListByOwnerId/{ownerId}")
    @ApiOperation("根据业主id获取报修")
    public Result<List<EiRepairInfoDTO>> getRepairListByOwnerId(@PathVariable("ownerId") int ownerId) {
        List<EiRepairInfoDTO> list = eiRepairInfoService.getRepairListByOwnerId(ownerId);
        return new Result<List<EiRepairInfoDTO>>().ok(list);
    }

    @GetMapping("/getRepairById/{id}")
    @ApiOperation("根据业主id获取报修")
    public Result<EiRepairInfoDTO> getRepairById(@PathVariable("id")int id) {
        EiRepairInfoDTO repairById = eiRepairInfoService.getRepairById(id);
        return new Result<EiRepairInfoDTO>().ok(repairById);
    }

    @DeleteMapping("/deleteRepairById/{id}")
    @ApiOperation("根据业主id删除")
    public Result<Integer> deleteRepairById(@PathVariable("id") int id) {
        int i = eiRepairInfoService.deleteRepairById(id);
        return new Result<Integer>().ok(i);
    }

    @DeleteMapping("/deleteRepairList/{ids}")
    @ApiOperation("批量删除")
    public Result<Integer> deleteRepairList(@PathVariable("ids") List<Integer> ids) {
        int i = eiRepairInfoService.deleteRepairList(ids);
        return new Result<Integer>().ok(i);
    }

    @GetMapping("/getStateListById/{ids}")
    @ApiOperation("根据id获取处理状态集合")
    public Result<Integer> getStateListById(@PathVariable List<Integer> ids) {
        Integer stateListById = eiRepairInfoService.getStateListById(ids);
        return new Result<Integer>().ok(stateListById);
    }

    @GetMapping("/getRepairList")
    @ApiOperation("获取报修集合联查")
    public Result<List<EiRepairDTO>> getRepairList() {
        List<EiRepairDTO> repairList = eiRepairInfoService.getRepairList();
        return new Result<List<EiRepairDTO>>().ok(repairList);
    }
}